<template>
    <div>
        <el-form :model="word" :rules="rules" ref="word" label-width="100px" class="demo-ruleForm">
            <el-form-item label="标题" prop="title">
                <el-input v-model="word.title"></el-input>
            </el-form-item>
            <el-form-item label="内容" prop="con">
                <el-input type="textarea" v-model="word.con"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="curWord('word')">确定</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
import $api from '../../api/index'
export default {
    data(){
        return {
            word:{
                title:'',
                con:'',
                id:''
            },
            rules:{
                title: [
                    { required: true, message: '请输入活动名称', trigger: 'blur' }
                ],
                con: [
                    { required: true, message: '请输入活动名称', trigger: 'blur' }
                ]
            }
        }
    },
    created(){
        let editWord = JSON.parse(localStorage.getItem('editWord'));

        let {title,con,id} = editWord;

        this.word = {
            title,
            con,
            id
        }
    },
    methods:{
        curWord(formName){
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    //添加接口
                    let {title,con,id} = this.word;
                    let {s_id} = this.$route.query;
                    let fun = id?'updateWord':'createWord';
                    $api[fun]({title,con,s_id},id).then(res => {
                        if(res.code===1){
                            this.$message({
                                message: id?'文档修改成功':'文档创建成功',
                                type: 'success'
                            });
                            //数据初始化
                            this.word = {
                                title:'',
                                con:'',
                                id:''
                            };
                            localStorage.removeItem('editWord');
                            this.$router.push('/main/home/word');
                        }else{
                            this.$message.error(id?'文档修改失败':'文档创建失败');
                        }
                    })
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
            
        }
    }
}
</script>
